#include "bsp.h"

#include "board/pin_mux.h"
#include "bsp_delay.h"
#include "clock_config.h"
#include "libs/mimxrt1052/fsl_iomuxc.h"

int bsp_clock_init() {
  BOARD_InitBootClocks();
  const clock_enet_pll_config_t config = {.enableClkOutput = true, .enableClkOutput25M = false, .loopDivider = 1};
  CLOCK_InitEnetPll(&config);
  IOMUXC_EnableMode(IOMUXC_GPR, kIOMUXC_GPR_ENET1TxClkOutputDir, true);
  return 0;
}

int bsp_tick_init() {
  uint32_t ms_tick = SystemCoreClock / 1000;
  SysTick->LOAD    = ms_tick;
  SysTick->VAL     = 0x00;
  SysTick->CTRL |= (1 << SysTick_CTRL_ENABLE_Pos) | (1 << SysTick_CTRL_CLKSOURCE_Pos) | (1 << SysTick_CTRL_TICKINT_Pos);
	return 0;
}

int bsp_init() {
  /* initialize iomux */
  BOARD_InitPins();
  /* initialize the clock */
  bsp_clock_init();

  bsp_tick_init();
  return 0;
}

static volatile uint32_t m_tick = 0;
void tick_service() { m_tick++; }

uint32_t bsp_get_tick() { return m_tick; }